﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Xml;
using VersionComparer.Images;

namespace VersionComparer.DataModel.Xml
{
    public class XmlNodeItem : XmlItem
    {
        protected static Image elementImage = InitializeImage(ImageResources.Element);
        public XmlNodeItem()
        {
        }
        public XmlNodeItem(ComparableItem parent, XmlNode node)
            : base(parent, node)
        {
        }

        protected override System.Drawing.Image GetImage()
        {
            return elementImage;
        }

        protected override void BuildRtf(Base.RtfBuilder builder, bool differencesOnly, bool useParentColor)
        {

            builder.Append("<", Color.Blue);
            builder.Append(XmlNode.Name, Color.DarkRed);

            ComparableItem[] aux = SubItems;
            if (!differencesOnly || aux == null || aux.Length == 0)
            {
                aux = GetChilds();
            }
            List<ComparableItem> childs = new List<ComparableItem>(aux);
            XmlAttributeItem [] attributes = childs.OfType<XmlAttributeItem>().ToArray();

            foreach (var attribute in attributes)
            {
                childs.Remove(attribute);
                attribute.GetAsRtf(builder, differencesOnly, useParentColor);
            }
            if (childs.Count == 0)
                builder.AppendLine("/>");
            else
            {
                
                builder.Append(">");
                if (!(childs[0] is XmlTextItem))
                    builder.AppendLine();
                builder.Ident();
                foreach (var node in childs)
                {
                    node.GetAsRtf(builder, differencesOnly, useParentColor);
                }
                builder.DeIdent();

                builder.Append("</", Color.Blue);
                builder.Append(XmlNode.Name, Color.DarkRed);
                builder.AppendLine(">", Color.Blue);
            }
        }
    }
}
